import java.util.Hashtable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

public final class ad extends ba {

    private cc a;
    private cc b;
    private ba c[];

    private static boolean a(StringBuffer stringbuffer, String s) {
        int i = stringbuffer.length();
        int j = s.length();
        if (i >= j) {
            i -= j;
            for (int k = 0; k < j; k++)
                if (stringbuffer.charAt(i + k) != s.charAt(k))
                    return false;

            return true;
        } else {
            return false;
        }
    }

    public ad(o o1) {
        super(o1);
        a = new cc();
        b = new cc();
    }

    public final void b() {
        b.b();
        a.b();
        c = new ba[0];
    }

    public final int g_() {
        return c.length;
    }

    public final ba a(int i) {
        return c[i];
    }

    private static o b(bu bu1, String s, Hashtable hashtable, String s1) {
        o o1 = null;
        if (hashtable != null) {
            o1 = (o) hashtable.get(s1);
        } else {
            if (bu1 != null) {
                if (s != null) {
                    o1 = bu1.a(s + s1);
                } else {
                    o1 = bu1.a(s1);
                }
            }
        }
        if (o1 == null)
            o1 = bh.d();
        return o1;
    }

    public final void a(bu bu1, String s, Hashtable hashtable, String s1) {
        cc cc1 = new cc();
        StringBuffer stringbuffer = new StringBuffer();
        int i = 0;
        int j = s1.length();
        do {
            if (i >= j)
                break;
            char c1;
            if ((c1 = s1.charAt(i++)) == '<') {
                if (stringbuffer.length() > 0) {
                    int k;
                    if ((k = cc1.a) > 0) {
                        o o1 = b(bu1, s, hashtable, (String) cc1.a(k - 2));
                        Object obj = cc1.a(k - 1);
                        a.e(new h(o1, stringbuffer.toString(), obj));
                    }
                    stringbuffer.setLength(0);
                }
                int l = i;
                while (i < j && s1.charAt(i++) != '>') ;
                String s2;
                int i1;
                if ((s2 = s1.substring(l, i - 1)).startsWith("/")) {
                    if ((i1 = cc1.a) > 0) {
                        String s3 = (String) cc1.a(i1 - 2);
                        if (s2.length() - s3.length() == 1 && s2.endsWith(s3)) {
                            cc1.c();
                            cc1.c();
                        }
                    }
                } else if ((i1 = s2.indexOf(' ')) > 0) {
                    cc1.e(s2.substring(0, i1));
                    cc1.e(s2.substring(i1 + 1).trim());
                } else {
                    cc1.e(s2);
                    cc1.e(null);
                }
            } else if (c1 == ';') {
                if (a(stringbuffer, "&lt")) {
                    stringbuffer.setLength(stringbuffer.length() - 3);
                    stringbuffer.append('<');
                } else if (a(stringbuffer, "&gt")) {
                    stringbuffer.setLength(stringbuffer.length() - 3);
                    stringbuffer.append('>');
                } else {
                    stringbuffer.append(';');
                }
            } else {
                stringbuffer.append(c1);
            }
        } while (true);
        z();
    }

    public final void a(o o1, String s) {
        a.e(new h(o1, s, null));
        z();
    }

    public final void a(o o1, String s, Object obj) {
        a.e(new h(o1, s, obj));
        z();
    }

    public final int g() {
        throw new RuntimeException("HyperText requires width != 0");
    }

    public final int g(int i) {
        return a(false, i);
    }

    private void a(cc cc1, int i, int j, int k, int l) {
        int i1 = super.I.e;
        int j1;
        if ((j1 = cc1.a - 1) >= 0)
            i1 = ((b) cc1.a(j1)).b.e;
        j1 = 0;
        switch (i1 & 0xd) {
            case 1: // '\001'
                j1 = (l - j) / 2;
                break;

            case 8: // '\b'
                j1 = l - j;
                break;
        }
        for (int k1 = cc1.a - 1; k1 >= 0; k1--) {
            b b1;
            if (((bk) (b1 = (b) cc1.a(k1))).e != i)
                return;
            b1.g = (short) k;
            b1.d += j1;
        }

    }

    private int a(boolean flag, int i) {
        int j = super.I.v;
        int k = 0;
        int l = 0;
        int i1 = 0;
        int j1 = 0;
        int k1 = 0;
        label0:
        for (int l1 = 0; l1 < a.a; l1++) {
            h h1;
            (h1 = (h) a.a(l1)).d = null;
            if (h1.c != null)
                j1++;
            o o1;
            int i2 = (o1 = h1.a).h();
            int j2 = o1.g();
            Font font = o1.j;
            String s;
            int k2 = (s = h1.b).length();
            i1 = Math.max(font.getHeight() + i2, i1);
            int l2 = 0;
            int i3 = 0;
            int j3 = 0;
            int k3 = 0;
            do {
                byte byte0;
                do {
                    byte0 = 0;
                    char c1;
                    if (l2 >= k2)
                        byte0 = 1;
                    else if (t.b(c1 = s.charAt(l2++))) {
                        byte0 = 2;
                    } else {
                        k3 += font.charWidth(c1);
                        if (k + j2 + k3 > i) {
                            if (j3 >= 0) {
                                l2 = j3;
                                byte0 = 2;
                            } else {
                                l2--;
                                byte0 = 2;
                            }
                        } else if (t.a(c1))
                            j3 = l2;
                    }
                } while (byte0 == 0);
                if (l2 > i3) {
                    int l3 = l2;
                    if (byte0 == 2 && t.d(s.charAt(l3 - 1)))
                        l3--;
                    if (l3 > i3) {
                        k3 = font.substringWidth(s, i3, l3 - i3) + j2;
                        k1 = Math.max(k1, l + i1);
                        if (flag) {
                            b b1 = new b(h1, k, l, k3, h1.a, s.substring(i3, l3));
                            b.e(b1);
                            if (h1.d == null)
                                h1.d = b1;
                        }
                        k += k3;
                    }
                }
                if (byte0 == 1)
                    continue label0;
                if (flag)
                    a(b, l, k, i1, i);
                k = 0;
                l += i1 + j;
                i1 = font.getHeight() + i2;
                i3 = l2;
                j3 = -1;
                k3 = 0;
            } while (true);
        }

        if (flag) {
            a(b, l, k, i1, i);
            c = new ba[j1];
        }
        return k1;
    }

    public final int a(int i, int j, int k, int l) {
        b.b();
        int i1 = a(true, k);
        int j1 = 0;
        for (int k1 = 0; k1 < a.a; k1++) {
            h h1;
            if ((h1 = (h) a.a(k1)).c != null) {
                c[j1++] = h1.c;
                bk bk1;
                (bk1 = h1.c.K).a(h1.d);
                bk1.d += i;
                bk1.e += j;
                h1.c.H = this;
            }
        }

        return i1;
    }

    public final void a(Graphics g1, bh bh1, o o1, int i, int j) {
        int k;
        int l = (k = g1.getClipY()) + g1.getClipHeight();
        ba ba1 = q().b;
        cc cc1 = b;
        int i1 = 0;
        for (int j1 = cc1.a; i1 < j1; i1++) {
            b b1;
            if (((bk) (b1 = (b) cc1.a(i1))).e + ((bk) (b1)).g < k)
                continue;
            if (((bk) (b1)).e >= l)
                return;
            b1.a(this, g1, ba1 != null && b1.a.c == ba1);
        }

    }

    public final boolean a(int i, int j, int k) {
        j -= super.K.d + super.I.c();
        k -= super.K.e + super.I.e();
        aj aj1 = q();
        cc cc1 = b;
        int l = 0;
        for (int i1 = cc1.a; l < i1; l++) {
            b b1;
            ba ba1;
            if ((ba1 = (b1 = (b) cc1.a(l)).a.c) != null && b1.a(j, k) && i == 1) {
                if (aj1.b == ba1)
                    bh.b().a(ba1, -5);
                else
                    aj1.a(ba1, 6);
                return true;
            }
        }

        return false;
    }
}